import sys, os, math, random
import wx
import wx.grid
from wx import adv

from GlobalMacro import *

class ToolPanel(wx.Panel):
    '''顶部信息窗口'''
    
    def __init__(self, parent):

        wx.Panel.__init__(self, parent)
        # 默认style是下列项的组合：wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER 
        # | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN 
        
        self.SetBackgroundColour(wx.Colour(124, 124, 124))
        #self.Center()

        self.data = [0.0] * 8
        self.titleFont = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
        self.labelFont = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL)
        
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        img_logo = wx.Image("splash2.jpg", wx.BITMAP_TYPE_ANY)
        self.logo_bmp = wx.Bitmap(img_logo.Scale(LOGO_W, INFOBAR_H))# 缩小图像
        
        self.InitBuffer()

        # Create a timer to update the data values
        self.Bind(wx.EVT_TIMER, self.OnTimeout)
        self.timer = wx.Timer(self)
        self.timer.Start(1000)

    def OnTimeout(self, evt):
        # print ("timer")
        data = []
        for d in self.data:
            val = d + random.uniform(-5, 5)
            if val < 0:
                val = 0
            if val > 110:
                val = 110
            data.append(val)
        self.data = data[:]
        self.Refresh()

    def OnSize(self, evt):
        # When the window size changes we need a new buffer.
        self.InitBuffer()

    def OnPaint(self, evt):
        # dc = wx.PaintDC(self)
        # self.DrawGraph(self.buffer)
        dc = wx.BufferedPaintDC(self, self.buffer)

    def InitBuffer(self):
        w, h = self.GetClientSize()        
        self.buffer = wx.Bitmap(w, h)
        dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
        self.DrawGraph(dc)

    def OnCloseWindow(self, event):
        self.Destroy()
    
    def PolarToCartesian(self, radius, angle, cx, cy):
        x = radius * math.cos(math.radians(angle))
        y = radius * math.sin(math.radians(angle))
        return (cx+x, cy-y)
    
    def DrawGraph(self, dc):
        spacer = 10
        scaledmax = 150.0
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        dw, dh = dc.GetSize()
        # print ("dw: %d; dh: %d" % (dw, dh))
        # Find out where to draw the title and do it
        #dc.SetFont(self.titleFont)
        #tw, th = dc.GetTextExtent(self.Title)
        #dc.DrawText(self.Title, int((dw-tw)/2), spacer)# 绘制标题
        th = 0
        # find the center of the space below the title
        # 找到中心点
        th = int(th + 2*spacer)
        cx = int(dw/2)
        cy = int((dh-th)/2) + th
        # calculate a scale factor to use for drawing the graph based
        # on the minimum available width or height
        # 计算比率
        mindim = min(cx, (dh-th)/2)
        scale = mindim/scaledmax
        # draw the graph axis and ”bulls-eye” with rings at scaled 25,
        # 50, 75 and 100 positions
        # 绘制轴线
        dc.SetPen(wx.Pen("black", 1))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.DrawCircle(cx, cy, int(25*scale))
        dc.DrawCircle(cx, cy, int(50*scale))
        dc.DrawCircle(cx, cy, int(75*scale))
        dc.DrawCircle(cx, cy, int(100*scale))
        dc.SetPen(wx.Pen("black", 2))
        dc.DrawLine(int(cx-110*scale), cy, int(cx+110*scale), cy)
        dc.DrawLine(cx, int(cy-110*scale), cx, int(cy+110*scale))

        # Now find the coordinates for each data point, draw the
        # labels, and find the max data point
        dc.SetFont(self.labelFont)
        maxval = 0
        angle = 0
        polypoints = []
        for i in range(8):
            val = self.data[i]
            point = self.PolarToCartesian(val*scale, angle, cx, cy)
            polypoints.append(point)
            x, y = self.PolarToCartesian(125*scale, angle, cx,cy)
            if val > maxval:
                maxval = val
            angle = angle + 360/8
            
        # Set the brush color based on the max value
        # (green is good, red is bad)
        c = "forest green"
        if maxval > 70:
            c = "yellow"
        if maxval > 95:
            c = "red"
        # Finally, draw the plot data as a filled polygon
        dc.SetBrush(wx.Brush(c))# 设置画刷颜色
        dc.SetPen(wx.Pen("navy", 3))
        dc.DrawPolygon(polypoints) #绘制采集的形状

        dc.DrawBitmap(self.logo_bmp, 0, 0)
